package cn.kongwg.filter;

import cn.kongwg.utils.JdbcUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2021-02-01 20:00
 */
@WebFilter("/*")
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {


        try {
            chain.doFilter(request, response);
            JdbcUtils.commitClose();

        } catch (Exception e) {
            JdbcUtils.rollbackClose();
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    @Override
    public void destroy() {

    }
}
